<template>
  <div>
    <el-row>
      <el-col>
        <ul class="tag-list">
          <li class="tag-item" v-for="(item, index) of tempNodes" :key="index">{{item.organizationName}}({{item.organizationCode}})</li>
        </ul>
        <div v-if="showClear" class="clear-tag" @click="clearAll"><fa-icon class="fa-close" /></div>
      </el-col>
    </el-row>
    <div class="top-panel__box">
      <el-form ref="dataForm" inline size="small" onsubmit="return false">
        <el-form-item>
          <el-input clearable placeholder="请输入机构名称搜索" v-model="filterTree"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="seachList">
            <fa-icon class="fa-search" />查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-row class="table-top">
      <el-col :span="24">
        <div style="height: 625px; overflow:auto">
          <el-tree ref="orgTreeDom"
          v-loading="tableLoading"
          :data="orgTree"
          show-checkbox
          :props="orgTreeProps"
          node-key="organizationId"
          default-expand-all
          highlight-current
          :expand-on-click-node="false"
          :filter-node-method="filterNode"
          @check-change="changeOrg"></el-tree>
        </div>
      </el-col>
    </el-row>
    <el-row class="btn-box">
      <el-button type="" @click="close" size="small">取消</el-button>
      <el-button type="primary" @click="submit" size="small">
        <fa-icon class="fa-check" />确定</el-button>
    </el-row>
  </div>
</template>

<script>
import api from '../api'
import { CONSTANT_OBJ } from '@/utils/constant'
import { mapState } from 'vuex'
export default {
  props: {
    valueList: Array
  },
  data () {
    return {
      tableLoading: false,
      showClear: false,
      filterTree: '',
      orgForm: {
        parentOrganizationId: '',
        pageNum: 1,
        pageSize: 10
      },
      listValue: [],
      orgTree: [],
      orgTreeProps: {
        children: 'childOrganizations',
        label: 'organizationName',
        disabled: true
      },
      tempIds: [],
      tempNodes: []
    }
  },
  mounted () {
    this.getTree()
  },
  methods: {
    getTree () {
      api.getQueryOrgTree().then(res => {
        const { responseCode, data } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.orgTree = data
          this.$store.commit('common/GET_ORG_TREE', data)
          if (this.valueList.length > 0) {
            this.$nextTick(() => {
              this.disposeNodeCheck(this.valueList)
            })
          }
        }
      })
    },
    disposeNodeCheck (list) {
      list.forEach(ele => {
        let node = this.$refs.orgTreeDom.getNode(ele)
        if (node.isLeaf) {
          this.$refs.orgTreeDom.setChecked(node, true)
        }
      })
    },
    filterNode (value, data) {
      if (!value) return true
      return data.organizationName.indexOf(value) !== -1
    },
    changeOrg (data, check, indeterminate) {
      console.log(data, check, indeterminate)
      this.tempIds = this.$refs.orgTreeDom.getCheckedKeys().concat(this.$refs.orgTreeDom.getHalfCheckedKeys())
      // if (!data.childList) {
      //   // this.tempNodes.push(data)
      // }
      let temp = [...this.tempNodes]
      let res = temp.filter(v => v.organizationId === data.organizationId)
      if (res.length > 0) {
        let result = temp.filter(ele => ele.organizationId !== data.organizationId)
        this.tempNodes = [...result]
      } else {
        this.tempNodes.push(data)
      }
      this.showClear = this.tempNodes.length > 0
    },
    submit () {
      let data = this.tempIds
      // console.log(data, '提交')
      // if (data.length === 0) {
      //   this.$message({
      //     message: '请选择机构',
      //     type: 'warning'
      //   })
      //   return false
      // }
      this.$emit('updataSuccess', data)
      this.$emit('updateTemp', this.tempNodes)
    },
    close () {
      this.$emit('close', false)
    },
    clearAll () {
      this.tempIds = []
      this.tempNodes = []
      this.showClear = false
      this.$refs.orgTreeDom.setCheckedKeys([])
    },
    seachList () {
      // this.getList(1)
    }
  },
  computed: {
    ...mapState({
      dicts: state => state.common.dicts
    })
  },
  watch: {
    filterTree: function (val) {
      this.$refs.orgTreeDom.filter(val)
    }
  }
}
</script>

<style lang="less">
.tag-list{
  width: 90%;
  min-height: 40px;
  border-radius: 4px;
  border: 1px solid #DCDFE6;
  list-style: none;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-wrap: wrap;
  box-sizing: border-box;
  padding: 5px 0;
  margin-bottom: 10px;
  .tag-item{
    display: inline-block;
    box-sizing: border-box;
    padding: 2px 10px;
    font-size: 12px;
    border: 1px solid #DCDFE6;
    margin: 0 5px 5px 5px;
    line-height: 22px;
    border-radius: 4px;
  }
}
.btn-box{
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
.clear-tag{
  position: absolute;
  right: 10px;
  top: 10px;
  z-index: 99;
  cursor: pointer;
}
</style>
